package com.yeqing.iodemo;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class 转换流 {

    public static void 读取utf8文件() {
        try (
                FileReader fileReader = new FileReader("/Users/cyq/Documents/utf8.txt")
        ) {
            int read;
            while ((read = fileReader.read()) != -1) {
                System.out.println((char) read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void 指定字符集解码字节() {
        try (
                FileInputStream fis = new FileInputStream("/Users/cyq/Documents/utf8.txt");
                FileInputStream fis2 = new FileInputStream("/Users/cyq/Documents/utf8.txt");
                FileInputStream fis3 = new FileInputStream("/Users/cyq/Documents/utf8.txt");
                InputStreamReader isr = new InputStreamReader(fis);
                InputStreamReader isr2 = new InputStreamReader(fis2, StandardCharsets.UTF_8);
                InputStreamReader isr3 = new InputStreamReader(fis3, "GBK");
        ) {
            int read;
            while ((read = isr.read()) != -1) {
                System.out.print((char) read);
            }
            System.out.println("");
            int read2;
            while ((read2 = isr2.read()) != -1) {
                System.out.print((char) read2);
            }
            System.out.println("");
            int read3;
            while ((read3 = isr3.read()) != -1) {
                System.out.print((char) read3);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void 写入不同的字符集文件() throws Exception {
        try(
                FileOutputStream  fos1 = new FileOutputStream("/Users/cyq/Develop/三国/黄忠.txt");
                OutputStreamWriter osw1 = new OutputStreamWriter(fos1);

                FileOutputStream  fos2 = new FileOutputStream("/Users/cyq/Develop/三国/公孙离.txt");
                OutputStreamWriter osw2 = new OutputStreamWriter(fos2, "GBK");
                ) {
            osw1.write("你好!");
            osw2.write("你好!");
        }
    }

    public static void main(String[] args) throws Exception {
//        读取utf8文件();

//        指定字符集解码字节();

        写入不同的字符集文件();
    }
}
